package com.naiterui.ehp.bs.b2c.order.service.impl;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.b2c.order.entity.ReturnOrderOperateLog;
import com.naiterui.ehp.bs.b2c.order.repository.IReturnOrderOperaterLogRepository;
import com.naiterui.ehp.bs.b2c.order.service.IReturnOrderOperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description 退换货订单操作日志业务实现类
 * @Author gongbaoqiang
 * @Date 2019/9/29 3:32 PM
 */
@Service
public class ReturnOrderOperateLogServiceImpl implements IReturnOrderOperateLogService {

    @Autowired
    private IReturnOrderOperaterLogRepository repository;

    @Override
    public void add(String businessNo, String operater) throws BusinessException {
        save(businessNo, operater, "新增");
    }

    @Override
    public void update(String businessNo, String operater) throws BusinessException {
        save(businessNo, operater, "修改");
    }

    @Override
    public void submit(List<String> businessNos, String operater) throws BusinessException {
        save(businessNos, operater, "提交");
    }

    @Override
    public void audit(List<String> businessNos, String operater) throws BusinessException {

        save(businessNos, operater, "审核");
    }

    @Override
    public void reset(List<String> businessNos, String operater) throws BusinessException {
        save(businessNos, operater, "反审");
    }

    @Override
    public void invalid(List<String> businessNos, String operater) throws BusinessException {
        save(businessNos, operater, "作废");
    }

    @Override
    public void confirm(List<String> businessNos, String operater) throws BusinessException {
        save(businessNos, operater, "确认收货");
    }

    /**
     * <pre>
     *     保存操作日志
     * </pre>
     * @param businessNo    业务单号
     * @param operater      操作人
     * @param operate       操作
     * @throws BusinessException
     */
    private void save(String businessNo, String operater, String operate) throws BusinessException{
        ReturnOrderOperateLog log = new ReturnOrderOperateLog();
        log.setBusinessNo(businessNo);
        log.setOperate(operate);
        log.setCreatedBy(operater);
        log.setCreatedAt(new Date());
        repository.save(log);
    }

    private void save(List<String> businessNos, String operater, String operate) throws BusinessException{
        List<ReturnOrderOperateLog> list = new ArrayList<>();
        Optional.of(businessNos).ifPresent(ls -> ls.forEach(s -> {
            ReturnOrderOperateLog log = new ReturnOrderOperateLog();
            log.setBusinessNo(s);
            log.setOperate(operate);
            log.setCreatedBy(operater);
            log.setCreatedAt(new Date());
            list.add(log);
        }));
        repository.save(list);
    }


}
